package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Author 喵喵喵
 * Date 2025/6/6 10:55
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void addDish(DishDTO dto) {
        //1,先新增菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.addDish(dish);
        //2.再新增口味
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor f : flavors) {
                f.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }


    }

    @Override
    public PageResult page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page=dishMapper.page(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteDishByIds(List<Long> ids) {
        //1.判断当前的菜品是否是启售状态,如果是,则抛出异常
        for (Long id : ids) {
            Dish dish=dishMapper.getDishById(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //2.判断当前的菜品是否被某个套餐所关联,如果是,则抛出异常
        Integer count = setmealDishMapper.getCountByDishId(ids);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3.删除菜品的操作
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO getDishById(Long id) {
        Dish dish = dishMapper.getDishById(id);
        List<DishFlavor> list=dishFlavorMapper.getDishFlavorByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Transactional
    @Override
    public void updateDish(DishDTO dto) {
        //1.修改菜品表的数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.updateDish(dish);

        //2.修改菜品口味表的数据(先删后增)
        dishFlavorMapper.deleteByDishId(dish.getId());
        List<DishFlavor> flavors=dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor f : flavors) {
                f.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public void StartORStopStatus(Integer status, Integer id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id.longValue())
                /*.updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();
        dishMapper.updateDish(dish);
    }

    @Override
    public List<DishVO> getDishesByCategoryId(Integer categoryId) {
        List<Dish> list = dishMapper.getDishesByCategoryId(categoryId);
        if (list != null && list.size() > 0){
            List<DishVO> dishVOList = new ArrayList<>();
            for (Dish dish : list) {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(dish,dishVO);
                dishVOList.add(dishVO);
            }
            return dishVOList;
        }
        return List.of();
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
        System.out.println(dishList);
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getDishFlavorByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


}
